// formPackage/pages/platformModule/commissionRuleList/commissionRuleList.js

import { queryMyCommissionRuleList, deleteOneCommissionRule } from "../../../../service/platform_service"

let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    dataList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
    this.queryMyCommissionRuleList();
  },

  //查询佣金规则列表
  queryMyCommissionRuleList() {
    wx.showLoading();
    queryMyCommissionRuleList().then(res => {
      wx.hideLoading();
      console.log("JJLog:佣金规则列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          dataList: res.data || []
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:queryMyCommissionRuleList", err);
    })
  },



  //新建佣金规则:
  createNewCommissionRule() {
    wx.navigateTo({
      url: '/formPackage/pages/platformModule/createNewCommissionRule/createNewCommissionRule',
    })
  },
  //跳转到佣金规则详情:
  pushToDetailAction(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/formPackage/pages/platformModule/createNewCommissionRule/createNewCommissionRule?id=' + id,
    })
  },

  //删除一个佣金规则:
  deleteOneCommission(e) {
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: "是否删除该报备规则？",
      confirmColor: "#FF0000",
      success: res => {
        if (res.confirm) {
          this.deleteOneCommissionRule(id, index);
        }
      }
    })
  },


  //删除佣金请求:
  deleteOneCommissionRule(id, index) {
    wx.showLoading();
    deleteOneCommissionRule(id).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: "删除成功",
          icon: "success"
        })
        let arr = this.data.dataList;
        arr.splice(index, 1);
        this.setData({
          dataList: arr
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:deleteOneCommissionRule", err);
    })
  },


})